package com.webssh.service.handler.terminal;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

public class TerminalSession {

    //key--->WebSocketSession value--->TerminalClient
    public static Map<WebSocketSession, TerminalClient> terminalSession = new ConcurrentHashMap<WebSocketSession, TerminalClient>(0);

    public static TerminalClient get(WebSocketSession key) {
        return terminalSession.get(key);
    }

    public static void put(WebSocketSession key, TerminalClient terminalClient) {
        terminalSession.put(key, terminalClient);
    }

    public static TerminalClient remove(WebSocketSession key) {
        return terminalSession.remove(key);
    }

    public static List<TerminalClient> findClient(Serializable httpSessionId) {
        List<TerminalClient> terminalClients = new ArrayList<TerminalClient>(0);
        if(httpSessionId == null)return terminalClients;
        if (terminalSession != null) {
            for (Map.Entry<WebSocketSession, TerminalClient> entry : terminalSession.entrySet()) {
                TerminalClient terminalClient = entry.getValue();
                if (terminalClient != null && terminalClient.getTerminal() != null) {
                    if (httpSessionId.equals(terminalClient.getHttpSessionId())) {
                        terminalClients.add(terminalClient);
                    }
                }
            }
        }
        return terminalClients;
    }
    
    /**
     * 通过已链接的ssh终端获取登陆信息Terminal
     * 
     * @author  zhanshuw
     * 			2018年6月22日
     * @param sessionId httpSessionId
     * @return
     * @throws IOException
     */
    public static Terminal findConnectedTerminalClient(String sessionId) {
    	if(sessionId != null){
    		for (Map.Entry<WebSocketSession, TerminalClient> entry : terminalSession.entrySet()) {
    			TerminalClient terminalClient = entry.getValue();
				if (sessionId.equals(terminalClient.getHttpSessionId())) {
					return terminalClient.getTerminal();
				}
    		}
    	}
    	return null;
    }
    
    public static TerminalClient findTerminalClientById(String clientId) {
    	if(clientId != null){
    		for (Map.Entry<WebSocketSession, TerminalClient> entry : terminalSession.entrySet()) {
    			TerminalClient terminalClient = entry.getValue();
    			if (clientId.equals(terminalClient.getTerminal().getClientId())) {
    				return terminalClient;
    			}
    		}
    	}
    	return null;
    }

    /**
     * 关闭所有httpSessionId的链接
     * @author  zhanshuw
     * 			2018年6月22日
     * @param httpSessionId
     * @throws IOException
     */
    public static void exit(String httpSessionId) throws IOException {
        if (terminalSession != null) {
            for (Map.Entry<WebSocketSession, TerminalClient> entry : terminalSession.entrySet()) {
                TerminalClient terminalClient = entry.getValue();
                if (terminalClient.getHttpSessionId().equals(httpSessionId)) {
                    terminalClient.disconnect();
                    terminalClient.getWebSocketSession().sendMessage(new TextMessage("Sorry! Session was invalidated, Terminal changed to closed. "));
                    terminalClient.getWebSocketSession().close();
                }
            }
        }
    }

}
